<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.min.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <link class="theme" rel="stylesheet" type="text/css" href="./css/color/blue.css" />
    <script type="text/javascript" src="./script/doT.min.js"></script>

</head>

<body>
    <div class="head">
        <div class="head-s">
            <span><div id='topbar'>市管领导干部信息查询系统--条件查询</div></span>
            <div class="head-r"><i class="fa fa-user-circle-o" onclick="user()"></i></div>
        </div>
    </div>
    <!-- <header class="aui-bar aui-bar-nav header">
        <a class="aui-pull-left">
        <span class="aui-iconfont aui-icon-left"></span>
    </a>
        <div class="aui-title"><span>市管领导干部信息查询系统--条件查询</span></div>
        <a class="aui-pull-right aui-btn">
            <span class="fa fa-user-circle-o" onclick="user()"></span>
        </a>
    </header> -->
    <div class="header-d"></div>
    <!-- <div class="list-t">
        <img src="images/cover_logo.png">
        <span>中共佳木斯市委组织部</span>
    </div> -->
    <div id="list">
    </div>
    <script type='text/template' id='template'>

        <div class="classify">
            {{~it :value:index}}
            <ul class="classify-t">
                {{? value.mflag==1}}
                <li>
                    <h3 onclick="click_in({{=value.ID}})">{{=value.PlanName}}</h3>
                </li>
                {{ } }}
            </ul>
            {{~}}
        </div>
    </script>
</body>
<script type="text/javascript" src="./script/api.js"></script>

</html>
<script>
    function click_in(n) {
      var db = api.require('CPDB');
        var partMember = 0;

        // var sql =
            // 'SELECT * from ( SELECT a.ID as userID,a.I01,a.I02,a.I03,a.I04,a.I05,a.I06,a.I07,a.I08,a.I09,a.I10,a.I11,a.I12,a.I13,a.I14,a.I15,a.I24,a.I25,a.I16,a.I45,a.I46,a.I53,b.Ldr_id,b.isort as ldrisort,c.ID,c.isort as partisort, d.ID, d.PID as d,d.isort as leveisort from Base_Info as a join base_dptldr as b join (select min(id) as id,ldr_id from Base_DptLdr group by ldr_id) as x join Base_Depart as c join Base_Level as d WHERE b.Ldr_id =a.ID AND c.ID = b.Dpt_ID AND c.PID = d.ID and b.id=x.id ) as b inner join Base_level as a WHERE a.ID = b.d AND';
   var sql1='';
   var sql2='';
   var sql3='';
   var tblname='';
            sql1='SELECT a.ID as userID,a.I01,a.I02,a.I03,a.I04,a.I05,a.I06,a.I07,a.I08,a.I09,a.I10,a.I11,a.I12,a.I13,a.I14,a.I15,a.I24,a.I25,a.I16,a.I45,a.I46,a.I53, ';
            sql1=sql1+'c.Ldr_id,c.isort as ldrisort,c.ID,c.isort as partisort, d.ID, d.isort as leveisort,c1.isort as leveisort0 ';
            sql1=sql1+'FROM Base_Depart d INNER JOIN ';
            sql1=sql1+'Base_DptLdr c ON d.ID = c.Dpt_ID INNER JOIN ';
            sql1=sql1+'Base_Level c1 ON d.PID = c1.ID INNER JOIN ';
            sql1=sql1+'(SELECT ID, Pid, dname, isort FROM Base_Level WHERE pid = 0) b ON c1.PID = b.ID INNER JOIN ';
            sql1=sql1+'Base_Info a ON c.Ldr_ID = a.ID INNER JOIN ';
            sql1=sql1+'(SELECT MIN(c.ID) AS ID, c.Ldr_ID FROM Base_Depart d INNER JOIN ';
            sql1=sql1+'Base_DptLdr c ON d .ID = c.Dpt_ID INNER JOIN ';
            sql1=sql1+'Base_Level c1 ON d .PID = c1.ID INNER JOIN ';
            sql1=sql1+'(SELECT ID, Pid, dname, isort FROM Base_Level WHERE pid = 0) b ON c1.PID = b.ID ';
            sql1=sql1+'inner JOIN Base_Info a ON c.Ldr_ID = a.ID ';
            sql1=sql1+'WHERE (1 = 1) ';
            sql2=' GROUP BY c.Ldr_ID) x ON c.ID = x.ID WHERE (1 = 1) ';
            sql3=' ORDER BY b.isort, c1.isort, ifnull(d.iSort, d.ID), c.iSort ';
//var sql1 = 'SELECT * from ( SELECT a.ID as userID,a.I01,a.I02,a.I03,a.I04,a.I05,a.I06,a.I07,a.I08,a.I09,a.I10,a.I11,a.I12,a.I13,a.I14,a.I15,a.I24,a.I25,a.I16,a.I45,a.I46,a.I53,b.Ldr_id,b.isort as ldrisort,c.ID,c.isort as partisort, d.ID, d.PID as d,d.isort as leveisort,y.isort as leveisort0 from Base_Info as a join base_dptldr as b join (SELECT MIN(c.ID) AS ID, c.Ldr_ID FROM Base_Depart d JOIN Base_DptLdr c ON d.ID = c.Dpt_ID JOIN Base_Level c1 ON d.PID = c1.ID JOIN (SELECT ID, Pid, dname, isort FROM Base_Level WHERE pid = 0) b ON c1.PID = b.ID  JOIN Base_Info a ON c.Ldr_ID = a.ID WHERE (1 = 1) AND ';
//var sql2 = ' GROUP BY c.Ldr_ID) as x join Base_Depart as c join Base_Level as d join (select id,isort from base_level where pid="0") as y ';
//  sql2 = sql2 + ' WHERE b.Ldr_id =a.ID AND c.ID = b.Dpt_ID AND c.PID = d.ID and b.id=x.id and y.id=d.pid ) as b inner join Base_level as a WHERE a.ID = b.d AND ';
  var sql='';
        db.selectSql({
            name: 'data',
            sql: 'SELECT * FROM Base_PlanDetail WHERE PID = ' + n,
        }, function(ret, err) {
            if (ret.status != false) {


                var newData = ret.data;
                for (var i = 0; i < newData.length; i++) {
                  // console.log(newData.length);
                  //
                  // if (newData[i]['ColName']=='RDSJ') {
                  //
                  //   if (newData[i].V2=='是' || newData[i].V2=='是') {
                  //     partMember = 1;
                  //   }else if (newData[i].V1=='否' || newData[i].V2=='否') {
                  //     partMember = 2;
                  //   }
                  //   continue;
                  // }
                    if (newData[i].Objname=='base_level')
                    {tblname ='b.';}
                    if (newData[i].Objname=='base_info')
                    {tblname ='a.';}
                    if (newData[i].Objname=='base_depart')
                    {tblname ='d.';}
                    if (newData[i].ColType == 'System.String') {
                        //取第一个
                        if (newData[i].E1 != '×') {
                            var sy;
                            //判断e1 符号
                            if (newData[i].E1 == '等于') {
                              if (newData[i].V1=='是') {
                           sql = sql + 'AND ' + ' a.i07*1<>0 ';
                         }else if (newData[i].V1=='否') {
                           sql = sql + 'AND '+ ' a.i07*1= 0 ';


                         }else {
                           sy = '==';
                           var d = tblname+ colName(newData[i].ColName);
                           sql = sql + ' AND ' + d + sy + " '" + newData[i].V1 + "' ";

                         }


                            } else if (newData[i].E1 == '＜') {
                                sy = '<';
                                var d = tblname+colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V1 + "' ";

                            } else if (newData[i].E1 == '＞') {
                                sy = '>';
                                var d = tblname+colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V1 + "' ";

                            } else if (newData[i].E1 == '包含') {
                                sy = ' LIKE ';
                                var d = tblname+colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '%" + newData[i].V1 + "%' ";
                            } else if (newData[i].E1 == '排除') {
                                sy = ' NOT LIKE ';
                                var d =tblname+ colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '%" + newData[i].V1 + "%' ";

                            } else {
                                sy = newData[i].E1;
                                var d =tblname+ colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V1 + "' ";
                            }

//                            if (i != newData.length - 1) {
//                              if ( partMember != 0) {
//
//                              }else {
//                                sql = sql + ' AND ';

//                              }
//                            }
                        }
                    } else {

                        if (newData[i].E1 != '×') {
                            var sy;
                            if (newData[i].E1 == '等于') {
                                sy = '==';
                                var d =tblname+ colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V1 + "' ";
                            } else if (newData[i].E1 == '＜') {
                                sy = '<';
                                var d = tblname+ colName(newData[i].ColName);
                                //
                                if (newData[i]['ColName'] == 'AGE') {
                                    sql = sql + ' AND substr((julianday(date())-julianday(i03))/365.25,1,2)*1 ' + sy + newData[i].V1 + " ";
                                } else {
                                    sql = sql + ' AND ' + d + sy + " '" + newData[i].V1 + "' ";
                                }

                            } else if (newData[i].E1 == '＞') {
                                sy = '>';
                                var d =tblname+  colName(newData[i].ColName);

                                if (newData[i]['ColName'] == 'AGE') {
                                    sql = sql + ' AND substr((julianday(date())-julianday(i03))/365.25,1,2)*1 ' + sy + newData[i].V1 + " ";
                                } else {
                                    sql = sql + ' AND ' + d + sy + " '" + newData[i].V1 + "' ";
                                }

                            } else if (newData[i].E1 == '包含') {
                                sy = ' LIKE ';
                                var d = tblname+ colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '%" + newData[i].V1 + "%' " + ' ';
                            } else if (newData[i].E1 == '排除') {
                                sy = ' NOT LIKE ';
                                var d = tblname+ colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '%" + newData[i].V1 + "%' " + ' ';

                            }

                            //计算年龄----------------------------------------------------------------------------
                            else if (newData[i].E1 == '≤') {

                                sy = ' <= ';
                                var d = tblname+ colName(newData[i].ColName);

                                if (newData[i]['ColName'] == 'AGE') {
                                    sql = sql + ' AND substr((julianday(date())-julianday(i03))/365.25,1,2)*1 ' + sy + newData[i].V1 + ' ';
                                } else {
                                  sql = sql + ' AND ' +d +' '+ sy+' '  + newData[i].V1 + ' ';
                                }
                            } else if (newData[i].E1 == '≥') {
                                sy = ' >= ';
                                var d =tblname+  colName(newData[i].ColName);
                                if (newData[i]['ColName'] == 'AGE') {
                                    sql = sql + ' AND substr((julianday(date())-julianday(i03))/365.25,1,2)*1 ' + sy + newData[i].V1 + ' ';
                                } else {
                                    sql = sql + ' AND ' +d +' '+ sy+' ' +  newData[i].V1 + ' ' ;
                                }
                            }
                            //计算年龄----------------------------------------------------------------------------
                            else {
                                sy = newData[i].E1;
                                var d =tblname+  colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V1 + "' ";
                            }

                            if (newData[i].E2 == '等于') {
                                sy = '==';
                                var d =tblname+  colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V2 + "' ";
                            } else if (newData[i].E2 == '＜') {
                                sy = '<';
                                var d = tblname+ colName(newData[i].ColName);
                                if (newData[i]['ColName'] == 'AGE') {
                                    sql = sql + ' AND substr((julianday(date())-julianday(i03))/365.25,1,2)*1 ' + sy + newData[i].V2;
                                } else {
                                    sql = sql + ' AND ' + d + sy + " '" + newData[i].V2;
                                }
                            } else if (newData[i].E2 == '＞') {
                                sy = '>';
                                var d = tblname+ colName(newData[i].ColName);

                                    if (newData[i]['ColName'] == 'AGE') {
                                sql = sql + ' AND substr((julianday(date())-julianday(i03))/365.25,1,2)*1 ' + sy + newData[i].V2 ;
                                } else {
                                  sql = sql + ' AND ' + d + sy + " '" + newData[i].V2;
                              }

                            } else if (newData[i].E2 == '包含') {
                                sy = ' LIKE ';
                                var d = tblname+ colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '%" + newData[i].V2 + "%' ";
                            } else if (newData[i].E2 == '排除') {
                                sy = ' NOT LIKE ';
                                var d =tblname+  colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '%" + newData[i].V2 + "%' ";

                            }
                            else if (newData[i].E2 == '≤') {

                                sy = ' <= ';
                                var d =tblname+  colName(newData[i].ColName);

                                if (newData[i]['ColName'] == 'AGE') {
                                    sql = sql + ' AND substr((julianday(date())-julianday(i03))/365.25,1,2)*1 ' + sy + newData[i].V2 ;
                                } else {
                                  sql = sql + ' AND ' +d +' '+ sy+' '  + newData[i].V2 ;
                                }
                            } else if (newData[i].E2 == '≥') {
                                sy = ' >= ';
                                var d = tblname+ colName(newData[i].ColName);
                                if (newData[i]['ColName'] == 'AGE') {
                                    sql = sql + ' AND substr((julianday(date())-julianday(i03))/365.25,1,2)*1 ' + sy + newData[i].V2  ;
                                } else {
                                    sql = sql + ' AND ' +d +' '+ sy+' ' +  newData[i].V2   ;
                                }
                            }

                             else {
                                sy = newData[i].E2;
                                var d =tblname+  colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V2 + "' ";
                            }

//                            if (i != newData.length - 1) {
//                                sql = sql + ' AND '
//                            }
                        } else {
                            //E1 =='×'

                            var sy;
                            if (newData[i].E2 == '等于') {
                                sy = '==';
                                var d =tblname+  colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V2 + "' ";
                            } else if (newData[i].E2 == '＜') {
                                sy = '<';
                                var d =tblname+  colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V2 + "' ";
                            } else if (newData[i].E2 == '＞') {
                                sy = '>';
                                var d = tblname+ colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V2 + "' ";
                            } else if (newData[i].E2 == '包含') {
                                sy = ' LIKE ';
                                var d = tblname+ colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '%" + newData[i].V2 + "%' ";
                            } else if (newData[i].E2 == '排除') {
                                sy = ' NOT LIKE ';
                                var d = tblname+ colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '%" + newData[i].V2 + "%' ";

                            } else {
                                sy = newData[i].E2;
                                var d = tblname+ colName(newData[i].ColName);
                                sql = sql + ' AND ' + d + sy + " '" + newData[i].V2 + "' ";
                            }

//                            if (i != newData.length - 1) {
//                                sql = sql + ' AND '
//                            }
                        }

                    } //newData[i].ColType=='System.DateTime' 结束

                } //for结束

                var tempsql = sql1 + sql +sql2 +sql + sql3;
                console.log(tempsql);

                // console.log(sql);
                api.showProgress({
                    title: '努力加载中...',
                    modal: false
                });
                api.openFrame({
                    name: 'fanansearch',
                    url: './fanansearch.html',
                    rect: {
                        x: 0,
                        y: 0,
                        w: 'auto',
                        marginBottom: 70
                    },
                    reload: true,
                    pageParam: {
                        sql: tempsql,
                        title: '市管领导干部信息查询系统--条件查询',
                        d: partMember
                    },
                    bounces: false,
                    bgColor: 'rgba(0,0,0,0)',
                    vScrollBarEnabled: true,
                    hScrollBarEnabled: true
                });
                // api.showProgress({
                //     title: '努力加载中...',
                //     // text: '先喝杯茶...',
                //     modal: false
                // });
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function colName(n) {

        if (n == 'CSNY') {
            n = ' I03 '
        } else if (n == 'XB') {
            n = ' I02 '
        } else if (n == 'MZ') {
            n = ' I04 '
        } else if (n == 'JG') {
            n = ' I05 '
        } else if (n == 'RDSJ') {
            n = ' I07 '
        } else if (n == 'CJGZSJ') {
            n = ' I08 '
        } else if (n == 'AGE') {
            n = ' I53 '
        } else if (n == 'QRZ') {
            n = ' I12 '
        } else if (n == 'BXXXZY') {
            n = ' I13 '
        } else if (n == 'ZZJY') {
            n = ' I14 '
        } else if (n == 'BYXXJZY') {
            n = ' I15 '
        } else if (n == 'XRZW') {
            n = ' I16 '
        } else if (n == 'QRZXL') {
            n = ' I24 '
        } else if (n == 'QRZXW') {
            n = ' I25 '
        }

        return n;
    }



    apiready = function() {
        var color = $api.getStorage('color');
        $(".theme").attr("href", color);
        var sql = 'select * from Base_Plan ORDER BY iSort '
        var db = api.require('CPDB');
        db.selectSql({
            name: 'data',
            sql: sql
        }, function(ret, err) {

            if (ret.status != false) {
                api.hideProgress();
                title = ret.data;
                var dotFn = doT.template(template.innerHTML);
                var html = dotFn(ret.data);
                var list = $api.byId('list');
                $api.html(list, html);
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function changeColor() {
        var color = $api.getStorage('color');
        $(".theme").attr("href", color);
    }

    function user() {
        api.openWin({
            name: 'user',
            url: './user.html',
        });
    }
</script>
